package com.flower.controller.common;
import com.flower.domain.dto.EmailDto;
import com.flower.utils.AjaxResult;
import com.flower.utils.EmailApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.UUID;

/**
 * 邮箱Controller
 */
@Slf4j
@RestController
@RequestMapping("/email")
@RequiredArgsConstructor
public class EmailController {

    private final EmailApi emailApi;
    private final StringRedisTemplate redisTemplate;

    private String getResetPasswordTemplate(String captcha) {
        return String.format("""
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <title>密码重置验证码</title>
            </head>
            <body style="margin: 0; padding: 0; background-color: #f8f9fa; font-family: 'Helvetica Neue', Arial, sans-serif;">
                <table width="100%%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f8f9fa">
                    <tr>
                        <td align="center" style="padding: 40px 0;">
                            <table width="650" border="0" cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-radius: 10px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);">
                                <!-- 顶部装饰条 -->
                                <tr>
                                    <td height="6" style="background: linear-gradient(90deg, #8e9aaf, #cbc0d3, #dee2ff, #efd3d7); border-top-left-radius: 10px; border-top-right-radius: 10px;"></td>
                                </tr>
                                
                                <!-- 头部Logo区域 -->
                                <tr>
                                    <td align="center" style="background-color: #8e9aaf; padding: 28px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.1);">
                                        <table width="85%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td align="center">
                                                    <table border="0" cellspacing="0" cellpadding="0">
                                                        <tr>
                                                            <td align="center" valign="middle" style="padding-right: 15px;">
                                                                <img src="https://love-fangfang.oss-cn-beijing.aliyuncs.com/images/2025/03/22/67dec3a18cce0508245ddc8c.png" alt="浪漫满屋" width="50" height="50" style="display: block; border: 0;" />
                                                            </td>
                                                            <td align="center" valign="middle">
                                                                <h1 style="margin: 0; color: white; font-size: 24px; font-weight: 500; letter-spacing: 1px;">密码重置</h1>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                
                                <!-- 内容区域 -->
                                <tr>
                                    <td style="padding: 40px;">
                                        <table width="100%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td style="color: #4a5568; font-size: 17px; line-height: 1.6; padding-bottom: 30px;">
                                                    尊敬的用户，我们已收到您的密码重置申请。请使用以下验证码完成密码重置：
                                                </td>
                                            </tr>
                                            
                                            <!-- 验证码区域 -->
                                            <tr>
                                                <td align="center" style="padding: 20px 0 35px 0;">
                                                    <table width="90%%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f7f8f9; border-radius: 10px; border: 1px solid #e2e8f0;">
                                                        <tr>
                                                            <td align="center" style="padding: 30px 20px;">
                                                                <span style="font-size: 36px; font-weight: 700; color: #516079; letter-spacing: 8px; font-family: 'Courier New', monospace;">%s</span>
                                                                <div style="width: 200px; height: 1px; background-color: #e2e8f0; margin: 20px auto 12px;"></div>
                                                                <p style="color: #8e9aaf; font-size: 13px; margin: 0; letter-spacing: 0.5px; font-weight: 500;">请勿向任何人透露此验证码</p>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            
                                            <tr>
                                                <td style="padding-bottom: 30px; color: #4a5568; font-size: 16px; line-height: 1.6;">
                                                    验证码有效期仅为<span style="color: #8e9aaf; font-weight: 500;">5分钟</span>，请尽快完成操作。
                                                </td>
                                            </tr>
                                            
                                            <!-- 安全提示 -->
                                            <tr>
                                                <td>
                                                    <table width="100%%" border="0" cellspacing="0" cellpadding="0" style="background-color: rgba(142, 154, 175, 0.07); border-radius: 10px; border-left: 4px solid #8e9aaf;">
                                                        <tr>
                                                            <td style="padding: 20px 25px;">
                                                                <h4 style="margin: 0 0 8px; color: #516079; font-size: 16px; font-weight: 600; letter-spacing: 0.3px;">安全提示</h4>
                                                                <p style="margin: 0; color: #4a5568; font-size: 15px; line-height: 1.6; letter-spacing: 0.2px;">
                                                                    如果这不是您的操作，您的账号可能存在安全风险，建议您立即修改密码或联系客服团队。
                                                                </p>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                
                                <!-- 底部区域 -->
                                <tr>
                                    <td align="center" style="background-color: #f5f7fa; padding: 25px 0; border-top: 1px solid #e2e8f0; border-bottom-left-radius: 10px; border-bottom-right-radius: 10px;">
                                        <table width="80%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td align="center" style="color: #8e9aaf; font-size: 14px; padding-bottom: 15px; font-weight: 500; letter-spacing: 0.3px;">
                                                    本邮件由系统自动发送，请勿回复
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                    <table border="0" cellspacing="0" cellpadding="0">
                                                        <tr>
                                                            <td style="font-size: 13px; color: #718096; letter-spacing: 0.3px;">
                                                                <span style="font-weight: 500;">浪漫满屋</span>
                                                                <span style="margin: 0 8px; color: #cbd5e0;">•</span>
                                                                <span>臻选花卉馆</span>
                                                                <span style="margin: 0 0 0 15px; font-size: 12px; color: #a0aec0;">© 2023 All Rights Reserved</span>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </body>
            </html>
            """, captcha);
    }

    private String getLoginVerificationTemplate(String captcha) {
        return String.format("""
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <title>登录验证码</title>
            </head>
            <body style="margin: 0; padding: 0; background-color: #f8f9fa; font-family: 'Helvetica Neue', Arial, sans-serif;">
                <table width="100%%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f8f9fa">
                    <tr>
                        <td align="center" style="padding: 40px 0;">
                            <table width="650" border="0" cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-radius: 10px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);">
                                <!-- 顶部装饰条 -->
                                <tr>
                                    <td height="6" style="background: linear-gradient(90deg, #7d8597, #adb5bd, #ccd4e0, #dbe1ed); border-top-left-radius: 10px; border-top-right-radius: 10px;"></td>
                                </tr>
                                
                                <!-- 头部Logo区域 -->
                                <tr>
                                    <td align="center" style="background-color: #7d8597; padding: 28px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.1);">
                                        <table width="85%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td align="center">
                                                    <table border="0" cellspacing="0" cellpadding="0">
                                                        <tr>
                                                            <td align="center" valign="middle" style="padding-right: 15px;">
                                                                <img src="https://love-fangfang.oss-cn-beijing.aliyuncs.com/images/2025/03/22/67dec3a18cce0508245ddc8c.png" alt="浪漫满屋" width="50" height="50" style="display: block; border: 0;" />
                                                            </td>
                                                            <td align="center" valign="middle">
                                                                <h1 style="margin: 0; color: white; font-size: 24px; font-weight: 500; letter-spacing: 1px;">登录安全验证</h1>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                
                                <!-- 内容区域 -->
                                <tr>
                                    <td style="padding: 40px;">
                                        <table width="100%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td style="color: #4a5568; font-size: 17px; line-height: 1.6; padding-bottom: 30px;">
                                                    尊敬的用户，欢迎使用浪漫满屋，请使用以下验证码完成登录验证：
                                                </td>
                                            </tr>
                                            
                                            <!-- 验证码区域 -->
                                            <tr>
                                                <td align="center" style="padding: 20px 0 35px 0;">
                                                    <table width="90%%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f7f8f9; border-radius: 10px; border: 1px solid #e2e8f0;">
                                                        <tr>
                                                            <td align="center" style="padding: 30px 20px;">
                                                                <span style="font-size: 36px; font-weight: 700; color: #516079; letter-spacing: 8px; font-family: 'Courier New', monospace;">%s</span>
                                                                <div style="width: 200px; height: 1px; background-color: #e2e8f0; margin: 20px auto 12px;"></div>
                                                                <p style="color: #7d8597; font-size: 13px; margin: 0; letter-spacing: 0.5px; font-weight: 500;">请勿向任何人透露此验证码</p>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            
                                            <tr>
                                                <td align="center" style="padding-bottom: 30px; color: #4a5568; font-size: 16px; line-height: 1.6;">
                                                    验证码有效期仅为<span style="color: #7d8597; font-weight: 500;">5分钟</span>，请尽快完成验证。
                                                </td>
                                            </tr>
                                            
                                            <!-- 安全提示 -->
                                            <tr>
                                                <td>
                                                    <table width="100%%" border="0" cellspacing="0" cellpadding="0" style="background-color: rgba(125, 133, 151, 0.07); border-radius: 10px; border-left: 4px solid #7d8597;">
                                                        <tr>
                                                            <td style="padding: 20px 25px;">
                                                                <h4 style="margin: 0 0 8px; color: #516079; font-size: 16px; font-weight: 600; letter-spacing: 0.3px;">安全提示</h4>
                                                                <p style="margin: 0; color: #4a5568; font-size: 15px; line-height: 1.6; letter-spacing: 0.2px;">
                                                                    为了保障您的账户安全，请勿将验证码泄露给他人。如果这不是您本人操作，请忽略此邮件并及时联系客服团队。
                                                                </p>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                
                                <!-- 底部区域 -->
                                <tr>
                                    <td align="center" style="background-color: #f5f7fa; padding: 25px 0; border-top: 1px solid #e2e8f0; border-bottom-left-radius: 10px; border-bottom-right-radius: 10px;">
                                        <table width="80%%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td align="center" style="color: #7d8597; font-size: 14px; padding-bottom: 15px; font-weight: 500; letter-spacing: 0.3px;">
                                                    本邮件由系统自动发送，请勿回复
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                    <table border="0" cellspacing="0" cellpadding="0">
                                                        <tr>
                                                            <td style="font-size: 13px; color: #718096; letter-spacing: 0.3px;">
                                                                <span style="font-weight: 500;">浪漫满屋</span>
                                                                <span style="margin: 0 8px; color: #cbd5e0;">•</span>
                                                                <span>臻选花卉馆</span>
                                                                <span style="margin: 0 0 0 15px; font-size: 12px; color: #a0aec0;">© 2023 All Rights Reserved</span>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </body>
            </html>
            """, captcha);
    }

    /**
     * 发送重置密码邮件
     *
     */
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody EmailDto emailDto) {
        String captchaString = UUID.randomUUID().toString().substring(0, 6);
        redisTemplate.opsForValue().set("resetPasswordCaptcha-"+ emailDto.getFrontendType()+":" + emailDto.getEmail(), captchaString, Duration.ofMinutes(5));
        log.info("发给{}的重置密码的验证码为：{}", emailDto.getEmail(), captchaString);
        String emailContent = getResetPasswordTemplate(captchaString);
        if (emailApi.sendGeneralEmail("重置密码验证码", emailContent, emailDto.getEmail())) {
            return AjaxResult.success(captchaString);
        }
        return AjaxResult.error("邮箱发送失败");
    }

    /**
     * 发送登录或注册邮件
     */
    @PostMapping("/into")
    public AjaxResult into(@RequestBody EmailDto emailDto) {
        String captchaString = UUID.randomUUID().toString().substring(0, 6);
        redisTemplate.opsForValue().set("captcha-"+ emailDto.getFrontendType()+":" + emailDto.getEmail(), captchaString, Duration.ofMinutes(5));
        log.info("发给{}的验证码为：{}", emailDto.getEmail(), captchaString);
        String emailContent = getLoginVerificationTemplate(captchaString);
        if (emailApi.sendGeneralEmail("登录验证码", emailContent, emailDto.getEmail())) {
            return AjaxResult.success(captchaString);
        }
        return AjaxResult.error("邮箱发送失败");
    }
}
